package com.check.operation.fragment;

import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.CheckApplication;
import com.check.operation.R;
import com.check.operation.base.BaseFragment;
import com.check.operation.model.Inpatient;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Message;
import com.check.operation.model.Yzzx;
import com.check.operation.service.YzzxAdapter;
import com.check.operation.util.PlaySoundPool;
import com.szj.util.HttpUtil;
import com.szj.util.JSONUtil;
import com.szj.util.SysUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.List;
import java.util.Map;

/**
 * 医嘱执行
 */
@EFragment(R.layout.fragment_yzzx)
public class YzzxFragment extends BaseFragment {
    public String action;
    public static final String TAG="医嘱执行";

    @App
    CheckApplication sshcApp;

    @ViewById
    TextView smh;

    @ViewById
    ExpandableListView yzlb;

    @ViewById
    TextView  zyh;

    @ViewById
    TextView xm;

    @ViewById
    TextView xb;

    @ViewById
    TextView ks;

    String tmsmh;
    final String sszt="i";
    final String ssztmc="医嘱执行";

    YzzxAdapter yzzxAdapter;

    PlaySoundPool playSoundPool;

    @AfterViews
    public void initview(){
        yzzxAdapter=new YzzxAdapter(getLayoutInflater(),sshcApp.getUserRe().getUser());
        yzlb.setAdapter(yzzxAdapter);
        playSoundPool=new PlaySoundPool(getActivity());
    }

    @Background
    public void refdownsj(final String zyh){
        try{
            initdata(zyh);
            String action="patient/patientinfo";
            Map<String,Object> params=sshcApp.getParams();

            params.put("user",sshcApp.getUserRe().getUser());
            params.put("request", new Object(){
                public String barCode=zyh;
            });
            String rs= HttpUtil.postJSON(action, JSONObject.toJSONString(params));
            if(SysUtil.isNotNUll(rs)){
                if(rs.indexOf("message")<0){
                    Message message=(Message) JSONUtil.fromJson(rs, Message.class);
                    alert(message);
                }else{
                    MessagRe<Inpatient> inp=JSONObject.parseObject(rs, new TypeReference<MessagRe<Inpatient>>(){});
                    uiInpat(inp);
                    action="order/getorders?inpatient_no="+ inp.value.inpatient_no;
                    rs=HttpUtil.get(action);
                    MessagRe<List<Yzzx>> yzzxre=JSONObject.parseObject(rs, new TypeReference<MessagRe<List<Yzzx>>>(){});
                    if("success".equals(yzzxre.message.code)){
                        yzzxAdapter.setYzzxs(yzzxre.value);
                        yzzxAdapter.setInpationt(inp.value.inpatient_no);
                        uiOrders();
                    }else{
                        alert(yzzxre.message.text);
                    }
                }
            }
        }catch(Exception e){
            alert(e.getMessage());
        }
    }

    @UiThread
    public void initdata(String tmh){
        smh.setText(tmh);
        zyh.setText(null);
        xm.setText(null);
        xb.setText(null);
        ks.setText(null);
    }

    @UiThread
    public void uiInpat(MessagRe<Inpatient> inp){
        Inpatient inpatient=inp.value;
        zyh.setText(inpatient.inpatient_no);
        xm.setText(inpatient.name);
        xb.setText(inpatient.sexname);
        ks.setText(inpatient.dept_name);
    }

    @UiThread
    public void uiOrders(){
        yzzxAdapter.notifyDataSetChanged();
    }

    @UiThread
    public void alert(Message message){
        Toast.makeText(getActivity(), message.text, Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void alert(String message){
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onGetBarValue(String arg0) {
        refdownsj(arg0);
    }

}
